package com.hongxun.design.ui.titlepopupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import android.widget.LinearLayout;
import android.widget.PopupWindow;

import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.ShareIconBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.imageutil.UriUtil;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;

/**
 * Created by zerdoor_pc on 2016/8/3.
 */
public class SharePop extends PopupWindow {
    private Context mContext;
    //列表弹窗的间隔
    protected final int LIST_PADDING = 20;
    //实例化一个矩形
    private Rect mRect = new Rect();
    //坐标的位置（x、y）
    private final int[] mLocation = new int[2];
    //屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;
    //判断是否需要添加或更新列表子类项
    private boolean mIsDirty;
    //位置不在中心
    private int popupGravity = Gravity.NO_GRAVITY;
    //弹窗子类项选中时的监听
    private OnItemOnClickListener mItemOnClickListener;
    //定义列表对象
    private GridView share_grid;
    //定义弹窗子类项列表
    private ArrayList<ShareIconBean> mActionItems = new ArrayList<ShareIconBean>();

    public SharePop(Context context) {
        //设置布局的参数
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public SharePop(Context context, int width, int height) {
        this.mContext = context;
        //设置可以获得焦点
        setFocusable(true);
        //设置弹窗内可点击
        setTouchable(true);
        //设置弹窗外可点击
        setOutsideTouchable(true);
        //获得屏幕的宽度和高度
        mScreenWidth = Util.getScreenWidth(mContext);
        mScreenHeight = Util.getScreenHeight(mContext);
        //设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);
        setBackgroundDrawable(new BitmapDrawable());
        //设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.pop_share, null));//设置pop的样式
        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI() {
        share_grid = (GridView) getContentView().findViewById(R.id.share_grid);
        share_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
                //点击子类项后，弹窗消失
                dismiss();
                if (mItemOnClickListener != null)
                    mItemOnClickListener.onItemClick(mActionItems.get(index), index);
            }
        });
    }

    /**
     * 显示弹窗列表界面
     */
    public void show(View view) {
        //获得点击屏幕的位置坐标
        view.getLocationOnScreen(mLocation);
        //设置矩形的大小
        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(), mLocation[1] + view.getHeight());
        //判断是否需要添加或更新列表子类项
        if (mIsDirty) {
            populateActions();
        }
        //显示弹窗的位置
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
       // showAtLocation(view, popupGravity, mScreenWidth - LIST_PADDING - (getWidth() / 2), mRect.bottom);//设置pop的位置

    }

    /**
     * 设置弹窗列表子项
     */
    private void populateActions() {
        mIsDirty = false;
        //设置列表的适配器
        share_grid.setAdapter(new BaseAdapter() {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                ItemHoller itemHoller = null;
                if (convertView == null) {
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.pop_item_share, null);//设置pop的item样式
                    itemHoller = new ItemHoller(convertView);
                    convertView.setTag(itemHoller);
                } else {
                    itemHoller = (ItemHoller) convertView.getTag();
                }

                ShareIconBean item = mActionItems.get(position);
                //设置文本文字
                itemHoller.name.setText(item.getName());
                SetTypeface.set(itemHoller.icon, item.getIcon());
                itemHoller.share_item.setOnClickListener(new MClickListener(position,item) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        ShareIconBean item = (ShareIconBean) msg;
                        MyApp.SHARE.shareCustom((Activity) mContext, item.getType(), title, url,intro,thumb);
                        dismiss();
                    }
                });


                    itemHoller.icon.setBackgroundResource(item.getBackground());

                return convertView;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Object getItem(int position) {
                return mActionItems.get(position);
            }

            @Override
            public int getCount() {
                return mActionItems.size();
            }
        });
    }


    class ItemHoller {
        public TextView icon;
        public TextView name;
        public LinearLayout share_item;
        ItemHoller(View convertView) {
            share_item= (LinearLayout) convertView.findViewById(R.id.share_item);
            icon = (TextView) convertView.findViewById(R.id.share_icon);
            name = (TextView) convertView.findViewById(R.id.share_title);
        }
    }
    String title="",  thumb="",  url="",intro="";
    public void setShare(String title,String thumb,String url,String intro){
        this.title=title;
        this.thumb=thumb;
        this.url=url;
        this.intro=intro;
    }

    /**
     * 添加子类项
     */
    public void addAction(ShareIconBean action) {
        if (action != null) {
            mActionItems.add(action);
            mIsDirty = true;
        }
    }

    /**
     * 清除子类项
     */
    public void cleanAction() {
        if (mActionItems.isEmpty()) {
            mActionItems.clear();
            mIsDirty = true;
        }
    }

    /**
     * 根据位置得到子类项
     */
    public ShareIconBean getAction(int position) {
        if (position < 0 || position > mActionItems.size())
            return null;
        return mActionItems.get(position);
    }

    /**
     * 设置监听事件
     */
    public void setItemOnClickListener(OnItemOnClickListener onItemOnClickListener) {
        this.mItemOnClickListener = onItemOnClickListener;
    }

    /**
     * @author yangyu
     *         功能描述：弹窗子类项按钮监听事件
     */
    public interface OnItemOnClickListener {
        void onItemClick(ShareIconBean item, int position);
    }
}
